package com.choosefine.it.project.controller;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.project.model.dto.ProjectCooperateBusDTO;
import com.choosefine.it.project.model.dto.ProjectCooperateBusSaveDTO;
import com.choosefine.it.project.model.vo.ProjectCooperateBusVO;
import com.choosefine.it.project.model.vo.SupplierInfoVO;
import com.choosefine.it.project.service.ProjectCooperateBusService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Comments：项目合作商家相关
 * Author：maojun
 * Create Date：2017/3/10
 * Modified By：
 * Modified Date：
 * Why & What is modified：
 * Version：v1.0
 */
@Api(value = "项目基本信息—项目合作商家",description = "毛俊")
@RestController
@RequestMapping("/basedata/projectinfo")
public class ProjectCooperateBusController extends BaseController {


    @Autowired
    ProjectCooperateBusService projectCooperateBusService;

    /**
     * @Author maojun
     * @Date 2017/2/15 15:10
     */
    @ApiOperation(value = "查询项目合作商家", notes = "控制台-项目管理-项目基本资料", response = ProjectCooperateBusVO.class)
    @RequestMapping(value = "/projectCooperateBus/queryProjectCooperateBus", method = RequestMethod.POST, produces = "application/json")
    public String queryProjectCooperateBus(@RequestBody ProjectCooperateBusDTO projectCooperateBusDTO) throws Exception {
        String result = null;
        Map<String, Object> params = new HashMap<>();
        if (projectCooperateBusDTO.getCompanyName() != null && !"".equals(projectCooperateBusDTO.getCompanyName())) {
            params.put("companyName", projectCooperateBusDTO.getCompanyName().trim());
        }
        if (projectCooperateBusDTO.getCorporation() != null && !"".equals(projectCooperateBusDTO.getCorporation())) {
            params.put("corporation", projectCooperateBusDTO.getCorporation().trim());
        }
        if (projectCooperateBusDTO.getProjectCode() != null && !"".equals(projectCooperateBusDTO.getProjectCode())) {
            params.put("projectCode", projectCooperateBusDTO.getProjectCode().trim());
        }
        if (projectCooperateBusDTO.getTenantRole() != null && !"".equals(projectCooperateBusDTO.getTenantRole())) {
            params.put("tenantRole", projectCooperateBusDTO.getTenantRole());
        }
        if (projectCooperateBusDTO.getUserCode() != null && !"".equals(projectCooperateBusDTO.getUserCode())) {
            params.put("userCode", projectCooperateBusDTO.getUserCode().trim());
        }
        PageDTO pageDto = new PageDTO();
        pageDto.setPageNum(projectCooperateBusDTO.getPageNum());
        pageDto.setPageSize(projectCooperateBusDTO.getPageSize());
        Page<ProjectCooperateBusVO> projectCooperateBusList = projectCooperateBusService.queryProjectCooperateBus(params, pageDto);
        if (projectCooperateBusList.size() > 0) {
            result = responseSuccess(projectCooperateBusList.toPageInfo());
        } else {
            result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
        }
        return result;
    }

    /**
     * @Author maojun
     * @Date 2017/2/15 16:55
     */
    @ApiOperation(value = "项目合作商家—选择供应商—查询", notes = "控制台-项目管理-项目基本资料", response = SupplierInfoVO.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "supplierType", value = "供应商类型(6:材料供应商；7:设备供应商；8:设施供应商)", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "codeOrName", value = "供应商租户编号或企业名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "起始页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", required = true, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/projectCooperateBus/querySupplier", method = RequestMethod.GET, produces = "application/json")
    public String querySupplier(Integer supplierType,
                                String codeOrName,
                                @RequestParam String projectCode,
                                @RequestParam int pageNum,
                                @RequestParam int pageSize) throws Exception {
        String result = null;
        Map<String, Object> params = new HashMap<>();
        if (supplierType != null) {
            params.put("supplierType", supplierType);
        }
        if (codeOrName != null && !"".equals(codeOrName)) {
            params.put("codeOrName", codeOrName.trim());
        }
        if (projectCode != null && !"".equals(projectCode)) {
            params.put("projectCode", projectCode.trim());
        }
        PageDTO pageDto = new PageDTO();
        pageDto.setPageNum(pageNum);
        pageDto.setPageSize(pageSize);
        Page<SupplierInfoVO> supplierInfoList = projectCooperateBusService.querySupplier(params, pageDto);
        if (supplierInfoList.size() > 0) {
            result = responseSuccess(supplierInfoList.toPageInfo());
        } else {
            result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
        }
        return result;
    }

    /**
     * @Author maojun
     * @Date 2017/2/16 9:50
     */
    @ApiOperation(value = "项目合作商家——解除/恢复项目合作商家关系", notes = "控制台-项目管理-项目基本资料")
    @ApiImplicitParams({@ApiImplicitParam(name = "ides", value = "项目合作商家关系id，多个id用逗号隔开", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "isValid", value = "0:恢复合作商家关系，1:解除合作商家关系", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/projectCooperateBus/changePartnership/{ides}/{isValid}", method = RequestMethod.GET, produces = "application/json")
    public String changePartnership(@PathVariable String ides, @PathVariable Integer isValid) {
        String result = null;
        String[] ids = ides.split(",");
        Map<String, Object> params = new HashMap<>();
        params.put("utime", DateUtils.getNowTime());
        params.put("ids", ids);
        params.put("isValid", isValid);
        Integer num = projectCooperateBusService.changePartnership(params);
        if (num != null && num > 0) {
            result = responseSuccess(num, ResultCode.SUCCESS_CREATED, ResultCode.MSG_UPDATE_SUCCESS);
        } else {
            result = responseFail(ResultCode.MSG_ERROR);
        }
        return result;
    }

    /**
     * @Author maojun
     * @Date 2017/2/16 15:18
     */
    @ApiOperation(value = "项目合作商家——新增项目合作商家", notes = "控制台-项目管理-项目基本资料")
    @RequestMapping(value = "/projectCooperateBus/saveProjectCooperateBus", method = RequestMethod.POST, produces = "application/json")
    public String saveProjectCooperateBus(@RequestBody List<ProjectCooperateBusSaveDTO> list) {
        String result = null;
        Integer num = projectCooperateBusService.saveProjectCooperateBus(list);
        if (num != null && num > 0) {
            result = responseSuccess(num, ResultCode.SUCCESS_CREATED, ResultCode.MSG_CREATE_SUCCESS);
        } else {
            result = responseFail(ResultCode.MSG_ERROR);
        }
        return result;
    }
}
